var webpack = require('webpack');
var path = require("path");

var CleanWebpackPlugin = require('clean-webpack-plugin');
var HtmlWebpackPlugin = require('html-webpack-plugin');

var ROOT_PATH = path.resolve(__dirname);
var APP_PATH = path.resolve(ROOT_PATH, "app");
var BUILD_PATH = path.resolve(ROOT_PATH, "dist");

const vendors = ['react', 'react-dom', 'react-router', 'redux', 'react-redux', 'react-router-redux', 'redux-actions', 'redux-promise', 'axios', 'antd'];
const app = path.resolve(APP_PATH, "./index.js");

module.exports = {
    entry: {
        vendors,
        app
    },
    output: {
        path: BUILD_PATH,
        filename: 'assets/[name].[hash].js'
    },
    module: {
        loaders: [
            {
                test: /\.html$/,
                loader: "html"
            },
            {
                test: /\.css$/,
                loaders: ["style", "css"]
            },
            {
                test: /\.scss$/,
                loaders: ["style", "css", "sass"]
            },
            {
                test: /\.less$/,
                loaders: ["style", "css", "less"]
            },
            {
                test: /\.jsx$|\.js/,
                exclude: /(node_modules|bower_components)/,
                loader: 'babel',
                query: {
                    presets: ['es2015', 'stage-0', 'react'],
                    plugins: [
                        ['import', [{
                            libraryName: "antd"/*,
                            style: true*/
                        }]]
                    ],
                    cacheDirectory: true
                }
            }
        ]
    },
    sassLoader: {
        data: "$env: " + process.env.NODE_ENV + ";"
    },
    plugins: [
        new webpack.DefinePlugin({
            'process.env': {
                'NODE_ENV': '"production"'
            }
        }),
        new CleanWebpackPlugin(["dist", "tmp"]),
        //这个使用uglifyJs压缩你的js代码
        new webpack.optimize.UglifyJsPlugin({
            minimize: true
        }),
        //把入口文件里面的数组打包成verdors.js
        new webpack.optimize.CommonsChunkPlugin('vendors', 'assets/vendors.[hash].js'),
        new HtmlWebpackPlugin({
            filename: 'index.html',
            template: path.resolve(APP_PATH, 'index.production.html'),
            hash:true,
            chunks: ['vendors', 'app']
        })
    ]
};